**************************************************
* FIRM-LEVEL REGRESSIONS FROM THE ECONOMIC CENSUS
**************************************************


*cap use "/Users/rchaure1/Dropbox/APcontract_reducedform (1)/economic_census_9805.dta", clear
noi di "Economic Census: Appending 1998 and 2005 Data"
use "$data/economic_census_1998", clear
append using "$data/economic_census_2005", force

noi di "Economic Census: Data Loaded"

* -- THIS SHOULD BE IN THE CLEANING FILE -- *
noi di "Economic Census: Cleaning Variables"
 gen treat = (state_id == 2 & year ==1998) | (state_id == 28 & year == 2005)
 gen post = year>=2003
 gen posttreat = post*treat
 
 gen unregistered = (registered==0)
 gen large = (L>=10)
 gen small = (L<10)

/*
egen L_cat = cut(L), at(1, 10, 20, 50,100, 50000) icodes
replace L_cat = L_cat + 1 
*replace L_cat = 0 if L == 1
qui ta L_cat, gen(Nc)
gcollapse (min) Lmin = L (max) Lmax = L (mean) registered (sum) Nc* if year == 2005, by(L_cat) fast
*/

*Manufacturing sector
gen manf = inrange(nic_2d,15,36)
gen sid_01 = int(did_01/100)
 
global controls = "own_*  "
global fe "did_01 year nic_2d sid_01##nic_2d power_fuel_used"
global cl "cl(sid_01)"

la var posttreat "Post $\times$ Treat"
*generating a consistent sample
qui reghdfe unregistered post treat posttreat $controls if manf == 1, absorb($fe) res(ehat)
gen sample = !missing(ehat)
est clear
 *keep if nic_2d>=15
 


********************************
* MANUFACTURING; YES CONTROLS
********************************
noi di "Economic Census: Manufacturing, With Controls"
qui eststo reg_myl: reghdfe unregistered post treat posttreat if large==1 & manf == 1 & sample == 1, absorb($fe $controls) $cl
    qui estadd local fes "Yes"
    qui estadd local controls "Yes"
    qui estadd local fsize "Large"
    qui estadd local sample "Manf."
    qui estadd scalar rsq = e(r2)
    qui estadd scalar samp = e(N)/1e6
    qui summ unregistered if post == 0 & treat == 0 & large == 1 & manf == 1 & sample == 1
    qui estadd scalar mean = r(mean)

qui eststo reg_mys: reghdfe unregistered post treat posttreat if small==1 & manf == 1 & sample == 1,  absorb($fe $controls) $cl
    qui estadd local fes "Yes"
    qui estadd local controls "Yes"
    qui estadd local fsize "Small"
    qui estadd local sample "Manf."
    qui estadd scalar rsq = e(r2)
    qui estadd scalar samp = e(N)/1e6
    qui summ unregistered if post == 0 & treat == 0 & small == 1 & manf == 1 & sample == 1
    qui estadd scalar mean = r(mean)

qui eststo reg_mya: reghdfe unregistered post treat posttreat if manf == 1 & sample == 1,  absorb($fe $controls) $cl
    qui estadd local fes "Yes"
    qui estadd local controls "Yes"
    qui estadd local fsize "All"
    qui estadd local sample "Manf."
    qui estadd scalar rsq = e(r2)
    qui estadd scalar samp = e(N)/1e6
    qui summ unregistered if post == 0 & treat == 0 & manf == 1 & sample == 1
    qui estadd scalar mean = r(mean)


drop ehat sample
qui reghdfe unregistered post treat posttreat, absorb($fe $controls) res(ehat)
gen sample = !missing(ehat)

********************************
* ALL INDUSTRIES; YES CONTROLS
********************************
noi di "Economic Census: All Sectors, With Controls"
qui eststo reg_ayl: reghdfe unregistered post treat posttreat if large==1 & sample == 1, absorb($fe $controls) $cl
    qui estadd local fes "Yes"
    qui estadd local controls "Yes"
    qui estadd local fsize "Large"
    qui estadd local sample "Manf."
    qui estadd scalar rsq = e(r2)
    qui estadd scalar samp = e(N)/1e6
    qui summ unregistered if post == 0 & treat == 0 & large == 1 & sample == 1
    qui estadd scalar mean = r(mean)

qui eststo reg_ays: reghdfe unregistered post treat posttreat if small==1 & sample == 1, absorb($fe $controls) $cl
    qui estadd local fes "Yes"
    qui estadd local controls "Yes"
    qui estadd local fsize "Small"
    qui estadd local sample "Manf."
    qui estadd scalar rsq = e(r2)
    qui estadd scalar samp = e(N)/1e6
    qui summ unregistered if post == 0 & treat == 0 & small == 1 & sample == 1
    qui estadd scalar mean = r(mean)

qui eststo reg_aya: reghdfe unregistered post treat posttreat if  sample == 1, absorb($fe $controls) $cl
    qui estadd local fes "Yes"
    qui estadd local controls "Yes"
    qui estadd local fsize "All"
    qui estadd local sample "Manf."
    qui estadd scalar rsq = e(r2)
    qui estadd scalar samp = e(N)/1e6
    qui summ unregistered if post == 0 & treat == 0 & sample == 1
    qui estadd scalar mean = r(mean)
		
 
*****************************
* OUTSHEETING THE RESULTS
*****************************

*Table 5
# delimit ;
esttab  reg_ay* using "$output/Table5.tex", replace
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Large" "Small" "All", pattern(1 1 1)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) nomtitles
stats(mean rsq samp fes controls, labels("Control, Pre Mean" "\midrule R2" "N (millions)" "\midrule Fixed Effects" "Controls")
fmt(2 2 2)) collabels(, none) mlabels(, none) nonumbers label booktabs nonotes
;
#delimit cr 


*Table A15
# delimit ;
esttab  reg_my* using "$ecoutput/TableA15.tex", replace
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Large" "Small" "All", pattern(1 1 1)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) nomtitles
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel A: Manufacturing Sector}}} \\ \midrule")
postfoot("")
stats(mean rsq samp, labels("Control, Pre Mean" "R2" "N")
fmt(2 2 2)) collabels(none) label booktabs nonotes
;









*************************************************
* HETEROGENEITY WITH FIRM SIZE ON LARGE - Table A14
*************************************************
foreach n in 10 20 30 50 100 {
    gen large`n' = (L>=`n')

noi di "Economic Census: Heterogeneity with `n'"
qui eststo reg_fh`n': reghdfe unregistered post treat posttreat if large`n'==1 & sample == 1, absorb($fe $controls) $cl
    qui estadd local fes "Yes"
    qui estadd local controls "Yes"
    qui estadd local fsize "Large"
    qui estadd scalar rsq = e(r2)
    qui estadd scalar samp = e(N)/1e6
    qui summ unregistered if post == 0 & treat == 0 & large`n' == 1 & sample == 1
    qui estadd scalar mean = r(mean)

}



# delimit ;
esttab  reg_ayl10 reg_ayl20 reg_ayl30 reg_fh50 reg_fh100 using "${ecoutput}/TableA14.tex", replace
keep(posttreat)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Probability of Working In Firm of Size", pattern(1 0 0 0 0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("= 10" "= 20" "= 30" "= 50" "= 100")
stats(mean rsq samp, labels("Control, Pre- Mean" "R2" "N (millions)")
fmt(2 2 2)) collabels(none) label booktabs nonotes
;
#delimit cr 



